﻿using BusyReports;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusyReportsConsole
{
    class Program
    {


        static int Main(string[] args)
        {
            if (args.Count() < 1)
            {
                Console.WriteLine("Invalid number of arguments!");
                Console.WriteLine("usage1: BusyReportsConsole ID where id is the id of the report (id column in the BusySubscriptions table)");
                Console.WriteLine("usage2: BusyReportsConsole PATH where Path is the path to the report (From the root in SSRS ReportManager, e.g. /AdventureWorks 2012/Customers_Near_Stores )");
                Console.WriteLine("usage3: Run all the reports matching the PATH (e.g. /AdventureWorks 2012/Customers%)");
                Console.WriteLine("use the BusyReportsGui to configure the UC4ReportDispatcher table");

                return -1;
            }

            BusyReportsViewModel myBusyReportsViewModel = new BusyReportsViewModel();
            Console.WriteLine(myBusyReportsViewModel.ConnectionInfo);

            List<BusySubscription> myBusySubscriptionsToRun = new List<BusySubscription>();

            addSubscriptionsToRun(args, myBusyReportsViewModel, myBusySubscriptionsToRun);

            myBusyReportsViewModel.run(myBusySubscriptionsToRun);

            System.Threading.Thread.Sleep(3000); // just to be sure it started

            myBusyReportsViewModel.waitForFinish();

            int i = myBusySubscriptionsToRun.Count(a => a.LastStatus.ToLower().StartsWith("error"));
            if (i > 0)
            {
                Console.WriteLine(String.Format("{0} Subscriptions FAILED ", i));
                return -1;
            }
            else
            {
                Console.WriteLine(String.Format("{0} Subscriptions executed ", myBusySubscriptionsToRun.Count()));
                return 0;
            }
        }

        private static void addSubscriptionsToRun(string[] args, BusyReportsViewModel myBusyReportsViewModel, List<BusySubscription> myBusySubscriptionsToRun)
        {


            foreach (string arg in args)
            {
                int myReportId = -1;
                if (Int32.TryParse(arg.ToString(), out myReportId))
                {
                    Console.WriteLine("Retrieving Subscription ID " + myReportId);
                    myBusySubscriptionsToRun.AddRange((from a in myBusyReportsViewModel.Subscriptions
                                                       where a.id == myReportId
                                                       select a).ToList<BusySubscription>());
                }
                else if (arg.EndsWith("%"))
                {
                    Console.WriteLine("Retrieving all Subscriptions for report " + arg.ToString());
                    myBusySubscriptionsToRun.AddRange((from a in myBusyReportsViewModel.Subscriptions
                                                       where a.ReportPath.StartsWith(arg.ToString().Replace("%", ""))
                                                       select a).ToList<BusySubscription>());
                }
                else
                {
                    Console.WriteLine("Retrieving all Subscriptions for report " + arg.ToString());
                    myBusySubscriptionsToRun.AddRange((from a in myBusyReportsViewModel.Subscriptions
                                                       where a.ReportPath == arg.ToString()
                                                       select a).ToList<BusySubscription>());
                }
            }

            Console.WriteLine("{0} Subscriptions found " + myBusySubscriptionsToRun.Count);
        }
    }
}
